package com.sskj.boom.ui.fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.boom.R2;
import com.sskj.boom.bean.MyGuessBean;
import com.sskj.boom.component.DaggerUserDataComponent;
import com.sskj.boom.presenter.MyGuessHistoryPresenter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.util.NumberUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;

import com.sskj.boom.R;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/27
 */
@Route(path = RConfig.BOOM_FRAGMENT_MY_GUESS_HISTORY)
public class MyGuessHistoryFragment extends BaseFragment<MyGuessHistoryPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter slimAdapter;
    private MVCCoolHelper<List<MyGuessBean>> mvcHelper;
    private Map<String, Integer> icons = new HashMap<>();

    @Override
    public int getLayoutId() {
        return R.layout.boom_fragment_my_guess_history;
    }

    @Override
    public MyGuessHistoryPresenter getPresenter() {
        return new MyGuessHistoryPresenter();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    @Override
    public void initView() {
        icons.put(Constans.COIN_USDT, R.mipmap.icon_usdt);
        icons.put(Constans.COIN_AICASH, R.mipmap.icon_ai);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        slimAdapter = SlimAdapter.create().register(R.layout.boom_item_my_guess, new SlimInjector<MyGuessBean>() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onInject(MyGuessBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_pid, data.getQs())
                        .image(R.id.iv_coin, icons.get(data.getPid_name()))
                        .text(R.id.tv_result, data.getBoomValue() + "X")
                        .text(R.id.tv_put, NumberUtil.keepNoZero(data.getBuy_money()))
                        .text(R.id.tv_income, data.getBack_money());
                switch (data.getStatus()) {
                    case 2:
                        injector.textColor(R.id.tv_income, ContextCompat.getColor(getActivity(), R.color.libUpColor))
                                .text(R.id.tv_escape_result, data.getFinal_beishu() + "X");
                        break;
                    case 3:
                        injector.textColor(R.id.tv_income, ContextCompat.getColor(getActivity(), R.color.libDownColor))
                                .text(R.id.tv_escape_result, data.getEscape_beishu() + "X");
                        break;
                }
                if (slimAdapter.getData().indexOf(data) % 2 == 0) {
                    injector.background(R.id.llBg, ContextCompat.getDrawable(App.INSTANCE, R.drawable.lib_solidf7f_cor10));
                } else {
                    injector.backgroundColor(R.id.llBg, ContextCompat.getColor(App.INSTANCE, R.color.transparent));
                }
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource<MyGuessBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<MyGuessBean>)
                        page -> mPresenter.getMyGuessList(page, 10), 10);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }


    @Override
    public void initData() {

    }

}
